/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : SimpleProcessHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 4, 2008       Raghu Sarraff			       Created
 */
package lwf.handler;

import java.util.ArrayList;
import java.util.List;

import lwf.factory.PropertyReader;
import lwf.handler.helper.DisplayFormatter;
import lwf.handler.helper.FormatAttribute;
import net.lwf.entity.AttemptedQuestion;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.util.ImageHtml;
import net.lwf.number.util.RandomNumberGenerator;
import net.lwf.string.util.StringManipulator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

/**
 * Type the class description here
 *  
 */
public class SimpleProcessHandler extends ProcessHandler implements IProcessHandler {
	
	private String questionDesc = null;
	private FormatAttribute format;
	
	public SimpleProcessHandler( String questiontype) {
		format = new FormatAttribute();
		this.initMLInWord();
	}
	
	/**
	 * @see net.lwf.handler.IProcessHandler#getDisplayFormatter()
	 */
	public DisplayFormatter getDisplayFormatter() {
		//format.setQuestionTable(this.generateQuestionTable());
		format.setQuestionNumber(getQuestionNum());
		format.setQuestionType(3);
		return new DisplayFormatter(format);
	}
	
	
	/**
	 * @see net.lwf.handler.IProcessHandler#getQuestionDesc()
	 */
	public String getQuestionDesc() {
		return this.questionDesc;
	}
	
	//Missing letter(s) in the word
	private void initMLInWord() {
		List lstwords =PropertyReader.getInstance().getAllRhymingWords();
		int indexPos = RandomNumberGenerator.generate(1, lstwords.size()-1);
		//WordInfo winfo = (WordInfo) lstwords.get(indexPos);
		String word = (String) lstwords.get(indexPos);
		//String word = winfo.getWord();
		int mLPosition = RandomNumberGenerator.generate(1, word.length());
		String findword = word.substring(mLPosition-1, mLPosition);
		setAnswer(findword);
		//String ansBox = "&nbsp;&nbsp;<input type=\"text\" style=\"font-size:20px;\" name=\"useranswer\" size=\"1\" onchange=\"checkAnswer()\"//>&nbsp;&nbsp;";
		//setQuestiontype(1);
        // Generate answer choices
		List lstData = new ArrayList();
		for(int i=0; i<word.length(); i++) {
			lstData.add(word.substring(i, i+1));
		}
		//setRadioAnswers(lstData);
		format.setRadioAnswers(lstData);
		//generate question string
		String qString = StringManipulator.replace(word, findword, "?");
		StringBuffer sbQBlock = new StringBuffer();
		String imagehtml = null;
		sbQBlock.append("<table width=\"100%\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">");
			sbQBlock.append("<tr>");
			sbQBlock.append("<td width=\"55%\">");
				sbQBlock.append("<table border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">");
					sbQBlock.append("<tr>");		
						for(int i=0; i<qString.length(); i++) {
						    String data = String.valueOf(qString.charAt(i));
						    if(data.equals("?")) {
								sbQBlock.append("<td class=\"question\" >");
									sbQBlock.append(data);
								sbQBlock.append("</td>");
						    }
						    else {
								sbQBlock.append("<td class=\"datacenter\">");
									sbQBlock.append(data);
								sbQBlock.append("</td>");				    	
						    }
						    if(i+1<qString.length()) {
						    	sbQBlock.append("<td width=\"10\"> &nbsp;</td>");	
						    }
						    else {
						    	imagehtml = ImageHtml.getWordImage(word);
						    }
							
						}
		            sbQBlock.append("</tr>");
		        sbQBlock.append("</table>");
		    sbQBlock.append("</td>");
		    sbQBlock.append("<td width=\"45%\">");
		    	sbQBlock.append(imagehtml);
		    sbQBlock.append("</td>");
		sbQBlock.append("</table>");
		this.questionDesc = sbQBlock.toString();
		format.setQuestionTable("Find the missing letter from the word below.", this.questionDesc);
	} //initMLInWord

	//------------------------------------------------ Methods for PDF generation on the browser
	public void generate(Document document) {
	
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return null;
	}

	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	public boolean matchAnswer(String useranswer, AttemptedQuestion aq) {
		return false;
	}

	@Override
	public String getPdfNote() {
		return null;
	}
	
} //SimpleProcessHandler

